import { api } from '@/api'
import vue from 'vue'
export default {
  namespaced: true,
  state: {},
  mutations: {
    UPDATE_DATA: (state, payload) => {
      for (let key in payload) {
        vue.set(state, key, payload[key])
      }
    }
  },
  actions: {
    setData ({ commit }, payload) {
      commit('UPDATE_DATA', payload)
    },
    sysInfo ({ commit, state }, forceUpdate = false) {
      return new Promise((resolve, reject) => {
        if (!state['systemInfo'] || forceUpdate) {
          return api.system.info().then(res => {
            commit('UPDATE_DATA', { systemInfo: res.data })
            resolve(res.data)
          }).catch(err => {
            reject(err)
          })
        } else {
          resolve(state['systemInfo'])
        }
      })
    },
    setDictionary ({ commit, state }, payload) {
      if (!state[payload]) {
        return api.base.dictionaryData(payload).then(res => {
          commit('UPDATE_DATA', { [payload]: res.data })
        })
      }
    },
    setPageParams ({commit}, payload) {
      commit('UPDATE_DATA', { pageParams:  payload})
    }
  }
}
